/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 15-10-21
 * Time: 下午8:34
 * To change this template use File | Settings | File Templates.
 */
(function($){
    //var showwait = true;
    //var beforeRender;//在渲染数据之前执行，接收到回调时立马执行。
    //var dataDiv;
    ////调用load方法需要传的参数
    //var data;//查询参数包括分页参数{page:10,max:12}
    //var url;//查询数据的地址
    //var fields;//[{name:"id",autoHidden:true,text:"编号",inTable:true,render:function(){}},{name:"name",autoHidden:false,text:"名称",inTable:true}]
    //var operations;//[{name:"remove",text:"删除",hadler:function(row){}},{name:"edit",text:"编辑",hadler:function(row){}}]
    //var dialogTitle="";//对话框title
    //var showDetail=true;
    //var webContext;//网站根目录
    //var async=true;
    //var afterLoad;
    //var loadSum;
    //var traditional;//是否深度解析
    //var renderBlock=false;
    //var reportShowFields;
    //
    ////去后台查询后返回结果存到这里的
    //var currentPage=0;
    //var pages;
    //var results;
    //var rows;
    //var offset=-1;
    //var max=-1;
    //var showFields;
    //
    //var scrollTopTemp=0;

    var optionsAndDataMap={};

    function renderDialog(i,divId){
        if (typeof($("#dataDetail"+divId)) != 'undefined') {
            $("#dataDetail"+divId).remove();
        }
        var row=optionsAndDataMap[divId].rows[i];

        var trs="";
        for(j=0;j<optionsAndDataMap[divId].fields.length;j++){
            var field=optionsAndDataMap[divId].fields[j];
            //alert("field.name->"+field.name);
            if(optionsAndDataMap[divId].fieldShows!=null){
                if(optionsAndDataMap[divId].fieldShows[field.name]==false){
                    continue;
                }
            }
            var text=field.text;
            var value=row[field.name];
            //alert("value->"+value);
            if(typeof(field.inDetail)=="undefined"||field.inDetail==true) {
                trs += "<tr>" +
                    "   <td>" + text + "</td>";
                if (typeof(field.render) != 'undefined') {
                    var index=i;
                    if(optionsAndDataMap[divId].offset>-1){
                        index+=optionsAndDataMap[divId].offset;
                    }
                    value = field.render(row,index);
                }
                if(value == null){
                    value = "";
                }
                trs += "   <td>" + value + "</td>";
                trs += "</tr>";
            }
        }


        var htmlStr=""+
            "<div class='modal fade bs-example-modal-lg' id='dataDetail"+divId+"' tabindex='-1' role='dialog' data-row='"+i+"' style='overflow:auto;overflow-y:scroll;'>                                                               "+
            "    <div class='modal-dialog modal-lg'>                                                                                                                 "+
            "        <div class='modal-content'>                                                                                                                     "+
            "            <div class='modal-header'>                                                                                                                  "+
            "                <button type='button' class='close' data-dismiss='modal'>&times;</button>                                                               "+
            "                <h4 class='modal-title' id='dinging_lable'>"+optionsAndDataMap[divId].dialogTitle+"</h4>                                                                                "+
            "            </div>                                                                                                                                      "+
            "            <div class='modal-body'>                                                                                                                    "+
            "                <div class='dinging-detailed'>                                                                                                          "+
            "                    <table class='table'>                                                                                                               "+
            trs;
//
//
//
//            "                        <tr>                                                                                                                            "+
//            "                            <td>编号</td>                                                                                                               "+
//            "                            <td data-value=''></td>                                                                                                    "+
//            "                        </tr>                                                                                                                           "+
//

        if(optionsAndDataMap[divId].operations.length>0){
            htmlStr+="                        <tr class='visible-xs'>                                                                                                         "+
                "                            <td>操作</td>  <td>";

            for(o=0;o<optionsAndDataMap[divId].operations.length;o++){
                var operation=optionsAndDataMap[divId].operations[o];
                htmlStr+="						<a href='#' name='"+operation.name+"' data-row='"+i+"' operation-id='"+o+"'>"+operation.text+"</a>";
            }

//            "                            <td><a href='#' name='edit' data-row='"+i+"'>编辑</a><a href='#' name='remove' data-row='"+i+"'>删除</a></td>                                               "+

            htmlStr+="                        </td></tr>                                                                                                                           ";
        }

        htmlStr+="                        <tr>                                                                                                                            "+
            "                            <td colspan='2'><button type='button' class='btn btn-primary' data-dismiss='modal' aria-hidden='true'>确定</button></td>    "+
            "                        </tr>                                                                                                                           "+
            "                    </table>                                                                                                                            "+
            "                </div>                                                                                                                                  "+
            "            </div>                                                                                                                                      "+
            "        </div>                                                                                                                                          "+
            "    </div>                                                                                                                                              "+
            "</div>                                                                                                                                                  ";

        var dialog = $(htmlStr);
        $("body").append(htmlStr);
        //添加事件处理
        for(o=0;o<optionsAndDataMap[divId].operations.length;o++){
            var operation=optionsAndDataMap[divId].operations[o];
            //alert(operation.name);
            $("#dataDetail"+divId+" a[name='"+operation.name+"']").click(function(){
                var rowId=$(this).attr("data-row");
                var operationId=$(this).attr("operation-id");
                //alert("rowId->"+rowId)
                var row=optionsAndDataMap[divId].rows[rowId];
                //alert(operationId);
                optionsAndDataMap[divId].operations[operationId].handler(row);
            });
        }
        optionsAndDataMap[divId].scrollTopTemp=document.body.scrollTop;
        //console.info("scrollTop"+scrollTopTemp);
        $("#dataDetail"+divId).modal('show');
        $("#dataDetail"+divId).unbind("hide.bs.modal");
        $("#dataDetail"+divId).on("hide.bs.modal",function(){
            document.body.scrollTop=optionsAndDataMap[divId].scrollTopTemp;
        });

        if(typeof(optionsAndDataMap[divId].afterRenderEach)!="undefined" ){
            optionsAndDataMap[divId].afterRenderEach();
        }
    }

    function render(divId){
        if(optionsAndDataMap[divId].rows==null||optionsAndDataMap[divId].rows.length==0){
            var htmlStr="<div style=\"margin: 15px 0px; text-align: center;\">"+
                "    没有数据！"+
                "</div>";
            optionsAndDataMap[divId].dataDiv.html(htmlStr);
        }else{
            //alert(fields.length);
            var theadTd="";
            for(i=0;i<optionsAndDataMap[divId].fields.length;i++){
                var field=optionsAndDataMap[divId].fields[i];
                //alert("field.text"+field.text);
                if(optionsAndDataMap[divId].fieldShows!=null){
                    if(optionsAndDataMap[divId].fieldShows[field.name]==false){
                        continue;
                    }
                }
                if(field.inTable){
                    if(field.autoHidden){
                        theadTd+="<td class='hidden-xs'>"+field.text+"</td>";
                    }else{
                        theadTd+="<td>"+field.text+"</td>";
                    }
                }
            }
            //alert("theadTd"+theadTd);
            var dataTr="";
            for(i=0;i<optionsAndDataMap[divId].rows.length;i++){
                var row=optionsAndDataMap[divId].rows[i];
                dataTr+="<tr>";
                for(j=0;j<optionsAndDataMap[divId].fields.length;j++){
                    var field=optionsAndDataMap[divId].fields[j];
                    //alert("field.name->"+field.name);
                    var value=row[field.name];
                    //alert("value->"+value);
                    if(field.inTable){
                        if(typeof(field.render)!='undefined'){
                            var index=i;
                            if(optionsAndDataMap[divId].offset>-1){
                                index+=optionsAndDataMap[divId].offset;
                            }
                            value=field.render(row,index);
                        }
                        if(value == null){
                            value = "";
                        }
                        if(field.autoHidden){
                            dataTr+="<td class='hidden-xs'>"+value+"</td>";
                        }else{
                            dataTr+="<td>"+value+"</td>";
                        }
                    }
                }
                if(optionsAndDataMap[divId].showDetail){
                    dataTr+="        <td class='hidden-xs'>";
                }else{
//                    dataTr+="        <td class=''>";
                }
                if(optionsAndDataMap[divId].showDetail){
                    dataTr+="						<a href='#' name='detail' data-row='"+i+"'>查看</a></td>";
                }

                if(optionsAndDataMap[divId].showDetail){
                    dataTr+="        <td class='visible-xs'>";
                    dataTr+="						<a class='dinging-examine' href='#' name='detail' data-row='"+i+"'>查看详细</a>";
                    dataTr+="				</td>";
                }
                if(optionsAndDataMap[divId].operations.length>0){
                    dataTr+="<td class='hidden-xs'>";
                    for(o=0;o<optionsAndDataMap[divId].operations.length;o++){
                        var operation=optionsAndDataMap[divId].operations[o];
                        dataTr+="						<a href='#' name='"+operation.name+"' data-row='"+i+"' operation-id='"+o+"'>"+operation.text+"</a>";
                    }
                    dataTr+="				</td>";
                }
                dataTr+="    </tr>";
            }
            //alert("dataTr->"+dataTr);
            var tableHtmlStr="<table class='table'>"+
                "    <thead>"+
                "        <tr>"+
                theadTd;
            if(optionsAndDataMap[divId].showDetail){
                tableHtmlStr+="            <td class='hidden-xs'>查看</td>";
                if(optionsAndDataMap[divId].operations.length>0){
                    tableHtmlStr+="            <td class='hidden-xs'>操作</td>";
                }
            }else{
//                tableHtmlStr+="            <td class=''>查看</td>";
                if(optionsAndDataMap[divId].operations.length>0){
                    tableHtmlStr+="            <td class='hidden-xs'>操作</td>";
                }
            }
            if(optionsAndDataMap[divId].showDetail){
                tableHtmlStr+="            <td class='visible-xs'>详细</td>";
//                tableHtmlStr+="            <td class='visible-xs'>操作</td>";
            }
            tableHtmlStr+="        </tr>"+
                "    </thead>"+
                "    <tbody>"+
                dataTr+
                "    </tbody>"+
                "</table>";
            var pageHtmlStr="<div class='right-table-pagination'>"+
                "<nav>"+
                "   <ul class='pager'> "+
                "           <li class='previous'><a href='#' name='prev'><i class='glyphicon glyphicon-arrow-left'></i>上一页</a></li>"+
                "       <span class='page'>"+
                "<select id='pagechanger"+divId+"'>" ;
            for(p=1;p<=optionsAndDataMap[divId].pages;p++){
                if(p==optionsAndDataMap[divId].currentPage){
                    pageHtmlStr+="<option value='"+p+"' selected>"+p+"</option>" ;
                }else{
                    pageHtmlStr+="<option value='"+p+"'>"+p+"</option>" ;
                }
            }
            pageHtmlStr+= "</select>"+
                "/"+optionsAndDataMap[divId].pages+"页<i class='hidden-xs'>，共"+optionsAndDataMap[divId].results+"条</i></span>"+
                "       <li class='next'><a href='#' name='next'>下一页<i class='glyphicon glyphicon-arrow-right'></i></a></li>"+
                "   </ul>"+
                "</nav>"+
                "</div>";
            //alert("tableHtmlStr->"+tableHtmlStr);
            var ts="<div class='hidden-xs' style=\"line-height:30px;height:30px;color:red;border:1px solid #ddd;padding-left:20px;\">在下面灰色区域，点击右键试试</div>";
            var htmlStr="";
            if(optionsAndDataMap[divId].showContextMenuTip){
                htmlStr=ts+tableHtmlStr;
            }else{
                htmlStr=tableHtmlStr;
            }
            if(optionsAndDataMap[divId].showPages){
                htmlStr+=""+pageHtmlStr;
            }
            optionsAndDataMap[divId].dataDiv.html(htmlStr);

            optionsAndDataMap[divId].showFields=new Array();
            //添加右击菜单
            var contextMenuItems={};
            for(i=0;i<optionsAndDataMap[divId].fields.length;i++){
                var field=optionsAndDataMap[divId].fields[i];
                if(optionsAndDataMap[divId].fieldShows!=null){
                    if(optionsAndDataMap[divId].fieldShows[field.name]==false){
                        continue;
                    }
                }
                if(field.inTable) {
                    contextMenuItems[field.name]={name: field.text, icon: "hook"};
                    optionsAndDataMap[divId].showFields.push(field.name);
                }else{
                    contextMenuItems[field.name]= {name: field.text, icon: ""};
                }
            }
            if(typeof (optionsAndDataMap[divId].reportShowFields)!="undefined"){
                optionsAndDataMap[divId].reportShowFields(optionsAndDataMap[divId].showFields);
            }
            if(optionsAndDataMap[divId].showContextMenu) {
                $.contextMenu("destroy",{selector:"#"+divId+ '>table>thead'});
                $("#context-menu-layer").remove();
                $.contextMenu('create',{
                    selector: "#"+divId+'>table>thead',
                    callback: function(key, options) {
                        for(i=0;i<optionsAndDataMap[divId].fields.length;i++){
                            var field=optionsAndDataMap[divId].fields[i];
                            if(optionsAndDataMap[divId].fieldShows!=null){
                                if(optionsAndDataMap[divId].fieldShows[field.name]==false){
                                    continue;
                                }
                            }
                            if(field.name==key) {
                                field.inTable=!field.inTable;
                                render(divId);
                                return true;
                            }
                        }
                    },
                    items:  contextMenuItems
                });
            }

            //绑定点击事件
            $("#pagechanger"+divId).change(function(){
                var selectPage=$(this).val();
                rload(selectPage,divId);
            });
            $("#"+divId+ " a[name='prev']").click(function(){
                if(optionsAndDataMap[divId].currentPage>1){
                    rload(optionsAndDataMap[divId].currentPage-1,divId);
                }
            });
            if(optionsAndDataMap[divId].currentPage<=1){
                $("#"+divId+ " a[name='prev']").parent().addClass("disabled");
            }
            $("#"+divId+ " a[name='next']").click(function(){
                if(optionsAndDataMap[divId].currentPage<optionsAndDataMap[divId].pages){
                    rload(optionsAndDataMap[divId].currentPage+1,divId);
                }
            });
            if(optionsAndDataMap[divId].currentPage>=optionsAndDataMap[divId].pages){
                $("#"+divId+ " a[name='next']").parent().addClass("disabled");
            }

            $("#"+divId+ " a[name='detail']").click(function(){
                var rowId=$(this).attr("data-row");
                //alert("rowId->"+rowId)
                var row=optionsAndDataMap[divId].rows[rowId];
                renderDialog(rowId,divId);
                if(optionsAndDataMap[divId].markClickRow!=null){
                    $(this).parent().parent().parent().find("tr").css("background-color","");
                    $(this).parent().parent().css("background-color",optionsAndDataMap[divId].markClickRow);
                }
            });

            for(o=0;o<optionsAndDataMap[divId].operations.length;o++){
                var operation=optionsAndDataMap[divId].operations[o];
                //alert(operation.name);
                $("#"+divId+ " a[name='"+operation.name+"']").click(function(){
                    var rowId=$(this).attr("data-row");
                    var operationId=$(this).attr("operation-id");
                    //alert("rowId->"+rowId)
                    var row=optionsAndDataMap[divId].rows[rowId];var preRow=null;
                    if(parseInt(rowId)>0){
                        preRow=optionsAndDataMap[divId].rows[parseInt(rowId)-1];
                    }
                    var nextRow=null;
                    if(parseInt(rowId)<optionsAndDataMap[divId].rows.length-1){
                        nextRow=optionsAndDataMap[divId].rows[parseInt(rowId)+1];
                    }
                    //alert(operationId);
                    optionsAndDataMap[divId].operations[operationId].handler(row,preRow,nextRow);
                    if(optionsAndDataMap[divId].markClickRow!=null){
                        $(this).parent().parent().parent().find("tr").css("background-color","");
                        $(this).parent().parent().css("background-color",optionsAndDataMap[divId].markClickRow);
                    }
                });
            }
        }
        if(typeof(optionsAndDataMap[divId].afterRenderEach)!="undefined" ){
            optionsAndDataMap[divId].afterRenderEach();
        }
    }

    function blockRender(divId){
        if(optionsAndDataMap[divId].rows==null||optionsAndDataMap[divId].rows.length==0){
            var htmlStr="<div style=\"margin: 15px 0px; text-align: center;\">"+
                "    没有数据！"+
                "</div>";
            optionsAndDataMap[divId].dataDiv.html(htmlStr);
        }else{
            //alert(fields.length);
            var dataTr="";
            var preRow=null;
            for(i=0;i<optionsAndDataMap[divId].rows.length;i++){
                var row=optionsAndDataMap[divId].rows[i];
                dataTr+="<div class=\"col-lg-3 col-md-3 col-sm-4 col-xs-6\">"+
                    "<div style=\"border: solid 1px #b8b8b8;padding: 5px;margin: 5px 0px;\">";
                for(j=0;j<optionsAndDataMap[divId].fields.length;j++){
                    var field=optionsAndDataMap[divId].fields[j];
                    //alert("field.name->"+field.name);
                    if(optionsAndDataMap[divId].fieldShows!=null){
                        if(optionsAndDataMap[divId].fieldShows[field.name]==false){
                            continue;
                        }
                    }
                    var value=row[field.name];
                    //alert("value->"+value);
                    if(field.inTable){
                        if(typeof(field.render)!='undefined'){
                            var index=i;
                            if(optionsAndDataMap[divId].offset>-1){
                                index+=optionsAndDataMap[divId].offset;
                            }
                            value=field.render(row,index,preRow);
                        }
                        if(value == null){
                            value = "";
                        }
                        if(field.autoHidden){
                            dataTr+=""+value+"";
                        }else{
                            dataTr+=""+value+"";
                        }
                    }
                }
                dataTr+="</div></div>";
                preRow=row;
            }
            //alert("dataTr->"+dataTr);

            var tableHtmlStr="<div class=\"row screen-group\">";

            tableHtmlStr+=dataTr;

            tableHtmlStr+="</div>";

            var pageHtmlStr="<div class='right-table-pagination'>"+
                "<nav>"+
                "   <ul class='pager'> "+
                "           <li class='previous'><a href='#' name='prev'><i class='glyphicon glyphicon-arrow-left'></i>上一页</a></li>"+
                "       <span class='page'>"+
                "<select id='pagechanger"+divId+"'>" ;
            for(p=1;p<=optionsAndDataMap[divId].pages;p++){
                if(p==optionsAndDataMap[divId].currentPage){
                    pageHtmlStr+="<option value='"+p+"' selected>"+p+"</option>" ;
                }else{
                    pageHtmlStr+="<option value='"+p+"'>"+p+"</option>" ;
                }
            }
            pageHtmlStr+= "</select>"+
                "/"+optionsAndDataMap[divId].pages+"页<i class='hidden-xs'>，共"+optionsAndDataMap[divId].results+"条</i></span>"+
                "       <li class='next'><a href='#' name='next'>下一页<i class='glyphicon glyphicon-arrow-right'></i></a></li>"+
                "   </ul>"+
                "</nav>"+
                "</div>";
            //alert("tableHtmlStr->"+tableHtmlStr);
            var htmlStr=tableHtmlStr;
            if(optionsAndDataMap[divId].showPages){
                htmlStr+=""+pageHtmlStr;
            }

            optionsAndDataMap[divId].dataDiv.html(htmlStr);

            //绑定点击事件
            $("#pagechanger"+divId).change(function(){
                var selectPage=$(this).val();
                rload(selectPage,divId);
            });
            $("#"+divId+ " a[name='prev']").click(function(){
                if(optionsAndDataMap[divId].currentPage>1){
                    rload(optionsAndDataMap[divId].currentPage-1,divId);
                }
            });
            if(optionsAndDataMap[divId].currentPage<=1){
                $("#"+divId+ " a[name='prev']").parent().addClass("disabled");
            }
            $("#"+divId+ " a[name='next']").click(function(){
                if(optionsAndDataMap[divId].currentPage<optionsAndDataMap[divId].pages){
                    rload(optionsAndDataMap[divId].currentPage+1,divId);
                }
            });
            if(optionsAndDataMap[divId].currentPage>=optionsAndDataMap[divId].pages){
                $("#"+divId+ " a[name='next']").parent().addClass("disabled");
            }

            $("#"+divId+ " a[name='detail']").click(function(){
                var rowId=$(this).attr("data-row");
                //alert("rowId->"+rowId)
                var row=optionsAndDataMap[divId].rows[rowId];
                renderDialog(rowId,divId);
                //alert("row->"+row);
//                var trs=$("#"+dialogId).find("table").find("tr");
//                //alert("trs->"+trs);
//                trs.each(function(){
//                    var td=$(this).find("td[fieldName]");
//                    var fieldName=td.attr("fieldName");
//                    //alert("fieldName->"+fieldName);
//                    var value=row[fieldName];
//                    td.attr("data-value",value);
//                    td.text(value);
//                    //alert("value->"+value);
//                });
//                //alert("dd");
//                $("#"+dialogId).modal('show');
            });

            for(o=0;o<optionsAndDataMap[divId].operations.length;o++){
                var operation=optionsAndDataMap[divId].operations[o];
                //alert(operation.name);
                $("#"+divId+ " a[name='"+operation.name+"']").click(function(){
                    var rowId=$(this).attr("data-row");
                    var operationId=$(this).attr("operation-id");
                    //alert("rowId->"+rowId)
                    var row=optionsAndDataMap[divId].rows[rowId];
                    var preRow=null;
                    if(parseInt(rowId)>0){
                        preRow=optionsAndDataMap[divId].rows[parseInt(rowId)-1];
                    }
                    var nextRow=null;
                    if(parseInt(rowId)<optionsAndDataMap[divId].rows.length-1){
                        nextRow=optionsAndDataMap[divId].rows[parseInt(rowId)+1];
                    }
                    //alert(operationId);
                    optionsAndDataMap[divId].operations[operationId].handler(row,preRow,nextRow);
                });
            }
        }
        if(typeof(optionsAndDataMap[divId].afterRenderEach)!="undefined" ){
            optionsAndDataMap[divId].afterRenderEach();
        }
    }
    function rload(page,divId){
        optionsAndDataMap[divId].data["page"]=page;
        if(typeof(optionsAndDataMap[divId].webContext)!='undefined'){
            $.doSubmit({
                type:optionsAndDataMap[divId].type,
                submitUrl:optionsAndDataMap[divId].url,
                data:optionsAndDataMap[divId].data,
                async:optionsAndDataMap[divId].async,
                traditional:optionsAndDataMap[divId].traditional,
                showwait:optionsAndDataMap[divId].showwait,
                callBack:function(response){
                    var redata=$.parseJSON(response);
                    optionsAndDataMap[divId].currentPage=redata.currentPage;
                    optionsAndDataMap[divId].pages=redata.pages;
                    optionsAndDataMap[divId].results=redata.results;
                    optionsAndDataMap[divId].rows=redata.rows;
                    optionsAndDataMap[divId].rowsInitial=new Array();
                    if(optionsAndDataMap[divId].rows!=null) {
                        for (var i = 0; i < optionsAndDataMap[divId].rows.length; i++) {
                            optionsAndDataMap[divId].rowsInitial.push($.extend(true, {}, optionsAndDataMap[divId].rows[i]));
                        }
                    }
                    optionsAndDataMap[divId].offset=redata.offset;
                    optionsAndDataMap[divId].max=redata.max;
                    if(typeof(optionsAndDataMap[divId].beforeRender) != 'undefined' ){
                        optionsAndDataMap[divId].beforeRender(redata);
                    }
                    if(optionsAndDataMap[divId].renderBlock){
                        blockRender(divId);
                    }else{
                        render(divId);
                    }
                    if (typeof($("#dataDetail"+divId)) != 'undefined') {
                        if($("#dataDetail"+divId).css("diplay")=="block"){
                            var i=$("#dataDetail"+divId).attr("data-row");
                            renderDialog(i,divId);
                        }
                    }

                    if(typeof(optionsAndDataMap[divId].afterLoad)!="undefined" ){
                        optionsAndDataMap[divId].afterLoad();
                    }
                    if(typeof(optionsAndDataMap[divId].loadSum) != 'undefined' ){
                        optionsAndDataMap[divId].loadSum(redata);
                    }
                },
                webContext:optionsAndDataMap[divId].webContext
            });
        }else{
            $.ajax({
                type: optionsAndDataMap[divId].type,
                url: optionsAndDataMap[divId].url,
                data: optionsAndDataMap[divId].data,
                async:optionsAndDataMap[divId].async,
                traditional:optionsAndDataMap[divId].traditional,
                showwait:optionsAndDataMap[divId].showwait,
                success:function(response){
                    if(typeof(response)!="object"&&response.indexOf("\<!DOCTYPE html\>")>0){
                        window.location.reload();
                        //return;
                    }
                    //alert(response);
                    var redata=$.parseJSON(response);
                    optionsAndDataMap[divId].currentPage=redata.currentPage;
                    optionsAndDataMap[divId].pages=redata.pages;
                    optionsAndDataMap[divId].results=redata.results;
                    optionsAndDataMap[divId].rows=redata.rows;
                    optionsAndDataMap[divId].rowsInitial=new Array();
                    if(optionsAndDataMap[divId].rows!=null) {
                        for (var i = 0; i < optionsAndDataMap[divId].rows.length; i++) {
                            optionsAndDataMap[divId].rowsInitial.push($.extend(true, {}, optionsAndDataMap[divId].rows[i]));
                        }
                    }
                    optionsAndDataMap[divId].offset=redata.offset;
                    optionsAndDataMap[divId].max=redata.max;
                    if(typeof(optionsAndDataMap[divId].beforeRender) != 'undefined' ){
                        optionsAndDataMap[divId].beforeRender(redata);
                    }
                    if(typeof(optionsAndDataMap[divId].loadSum) != 'undefined' ){
                        optionsAndDataMap[divId].loadSum(redata);
                    }
                    if(typeof(optionsAndDataMap[divId].afterLoad)!="undefined" ){
                        optionsAndDataMap[divId].afterLoad();
                    }
                },
                complete: function(e, xhr, settings){
                    //alert(e.status);
                    if(e.status === 200){
                        render(divId);
                        if (typeof($("#dataDetail"+divId)) != 'undefined') {
                            if($("#dataDetail"+divId).css("diplay")=="block"){
                                var i=$("#dataDetail"+divId).attr("data-row");
                                renderDialog(i,divId);
                            }
                        }
                    }else if(e.status === 304){

                    }else{

                    }
                }
            });
        }
    }
    function load(divId){
        rload(1,divId);
    }



    $.fn.datagridExpand=function(method,options){
        var divId;
        var dataDiv;
        this.each(function () {
            dataDiv=$(this);
            divId=dataDiv.attr("id");
        });
        if(typeof(divId)=="undefined" ){
            return;
        }

        if(method=="setFieldShow"){
            if(typeof(options.fieldShows) != 'undefined') {
                optionsAndDataMap[divId].fieldShows = options.fieldShows;
            }
            return;
        }
        if(method=="getRows"){
            return optionsAndDataMap[divId].rows;
        }
        if(method=="getRow"){
            var rowIndex=options.index;
            var row=null;
            if(typeof(optionsAndDataMap[divId].rows)!="undefined"&&typeof(rowIndex)!="undefined"){
                row=optionsAndDataMap[divId].rows[rowIndex];
            }
            return row;
        }
        if(method=="setRow"){
            var rowIndex=options.index;
            var row=options.row;
            if(typeof(optionsAndDataMap[divId].rows)!="undefined"&&typeof(rowIndex)!="undefined"){
                optionsAndDataMap[divId].rows[rowIndex]=row;
            }
            return;
        }
        if(method=="render"){
            if(optionsAndDataMap[divId].renderBlock){
                blockRender(divId);
            }else{
                render(divId);
            }
            return;
        }
        if(method=="resetRows"){
            if(optionsAndDataMap[divId].rowsInitial!=null){
                optionsAndDataMap[divId].rows=new Array();
                //console.info("rows->"+rows);
                for(var i=0;i<optionsAndDataMap[divId].rowsInitial.length;i++){
                    optionsAndDataMap[divId].rows.push($.extend(true, {}, optionsAndDataMap[divId].rowsInitial[i]));
                }
                //console.info("rows->"+rows);
            }
            return;
        }



        if(method=="load") {
            var defaults = {
                showwait: true,
                //beforeRender:null,//在渲染数据之前执行，接收到回调时立马执行。
                dataDiv: dataDiv,
                //调用load方法需要传的参数
                //data:null,//查询参数包括分页参数{page:10,max:12}
                //url:null,//查询数据的地址
                //fields:null,//[{name:"id",autoHidden:true,text:"编号",inTable:true,render:function(){}},{name:"name",autoHidden:false,text:"名称",inTable:true}]
                //operations:null,//[{name:"remove",text:"删除",hadler:function(row){}},{name:"edit",text:"编辑",hadler:function(row){}}]
                dialogTitle: "",//对话框title
                showDetail: true,
                //webContext:null,//网站根目录
                async: true,
                //afterLoad:null,
                //loadSum:null,
                //traditional:null,//是否深度解析
                renderBlock: false,
                //reportShowFields:null,
                //afterRenderEach:null,
                type:"post",

                //去后台查询后返回结果存到这里的
                currentPage: 0,
                //pages:null,
                //results:null,
                //rows:null,
                offset: -1,
                max: -1,
                //showFields:null,
                scrollTopTemp: 0,
                showPages:true,
                showContextMenuTip:true,
                showContextMenu:true,
                markClickRow:null
            };
            optionsAndDataMap[divId] = $.extend(defaults, options);
        }else if(method=="reload"){
            optionsAndDataMap[divId] = $.extend(optionsAndDataMap[divId], options);
        }
        return this.each(function () {
            dataDiv=$(this);
            if(method=="load"){
                load(divId);
            }else if(method=="reload"){
                rload(optionsAndDataMap[divId].currentPage,divId);
            }
        });
    }

})(jQuery);
